#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 3e5 + 10, INF = 0x3f3f3f3f;

int  n, a[N];

void f(int l, int r)
{
    if (l == r) return;
    int mid = (l + r) >> 1;
    f(l, mid);
    f(mid + 1, r);
    if (a[l] > a[mid + 1]) {
        int len = (r - l + 1) >> 1;
        for (int i = 0;i < len;i++)
            swap(a[l + i], a[mid + 1 + i]);
    }
}

void solve()
{
    cin >> n; n = 1 << n;
    for (int i = 1;i <= n;i++) cin >> a[i];

    f(1, n);

    for (int i = 1;i <= n;i++) cout << a[i] << ' ';
    cout << "\n";
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}